/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ua.lviv.franko.FEMShapeFunction1D;

import ua.lviv.franko.integration.IFunction1D;
import ua.lviv.franko.mlsFunctions.oneDimension.ICalculate;

/**
 * 
 * @author mark_o
 */
public class FEMShapeFunction1D1 implements IFunction1D, ICalculate, IFEM1D {
	double	xi;
	double	h;

	public FEMShapeFunction1D1(double xi, double h) {
		this.xi = xi;
		this.h = h;
	}

	public double calculate(double x) {
		if ((x < xi - h) || (x > xi + h)) {
			return 0.0;
		}
		if (x < xi) {
			return 1 + (x - xi) / h;
		} else {
			return 1 + (-x + xi) / h;
		}
	}

	@Override
	public double[] calculateAll(double x) {
		return new double[] { this.calculate(x), this.calculateDerivaty(x) };
	}

	public double calculateDerivaty(double x) {
		if ((x < xi - h) || (x > xi + h)) {
			return 0.0;
		}
		if (xi - x > 0) {
			return 1.0 / h;
		} else {
			return -1.0 / h;
		}
	}

	public double[] getBounds() {
		return new double[] { xi - h, (xi + h) };
	}

	public double getH() {
		return h;
	}

	public double getXi() {
		return xi;
	}

	public static void main(String[] args) {
		// ArrayList<Double> list = new ArrayList<Double>();
		double a = 0;
		double b = 0.5;
		int N = 100;
		double h = (b - a) / N;
		// for (int i = 0; i <= N; i++) {
		// list.add(a+h*i);
		// }
		// for(int j=0;j<=N;j++){
		// FEMShapeFunction1D1 f = new FEMShapeFunction1D1(h*j, h);
		// System.out.println("f([");
		// for (int i = 0; i <= N; i++) {
		// System.out.println((a+h*i)+"\t\t"+f.calculate(a+h*i));
		// }
		// System.out.println("])");
		// }
		FEMShapeFunction1D1 f = new FEMShapeFunction1D1(0.0, 0.25);
		for (int i = 0; i <= N; i++) {
			System.out.println((a + h * i) + "\t\t" + f.calculate(a + h * i));
		}

	}

}
